<?php


namespace app\api\services;


use app\api\model\TestHrmdepartment;
use app\api\model\TestHrmresource;
use app\api\model\TestUfhuikjl;

/**
 * Class TestService
 * @package app\api\services
 * @desc 测试服务类
 */
class TestService
{
    use ApiTrait;

    /**
     * @desc 用户回款数据统计
     * @param int $id 部门id
     */
    public function userReturnMoneyList($id)
    {
        $ret = [];
        $testUfhuikjl = new TestUfhuikjl();
        // 查询用户列表
        $whereu['departmentid'] = $id;
        $user = TestHrmresource::where($whereu)->order('id desc')->select()->toArray();
        // 查询部门表
        $depart = TestHrmdepartment::order('id desc')->select()->toArray();
        $departArr = array_column($depart,'departmentname','id');
//        halt($user,$departArr);
        foreach ($user as $v) {
            $retv['username'] = $v['lastname'];
            $retv['departmentname'] = $departArr[$v['departmentid']];

            // 每月回款统计
            $retv['oneMonth'] = $testUfhuikjl->getSum($v['id'],'2023-01-01','2023-02-01');
            $retv['twoMonth'] = $testUfhuikjl->getSum($v['id'],'2023-02-01','2023-03-01');
            $retv['threeMonth'] = $testUfhuikjl->getSum($v['id'],'2023-03-01','2023-04-01');
            $retv['fourMonth'] = $testUfhuikjl->getSum($v['id'],'2023-04-01','2023-05-01');
            $retv['fiveMonth'] = $testUfhuikjl->getSum($v['id'],'2023-05-01','2023-06-01');
            $retv['sixMonth'] = $testUfhuikjl->getSum($v['id'],'2023-06-01','2023-07-01');
            $retv['servenMonth'] = $testUfhuikjl->getSum($v['id'],'2023-07-01','2023-08-01');
            $retv['eightMonth'] = $testUfhuikjl->getSum($v['id'],'2023-08-01','2023-09-01');
            $retv['nineMonth'] = $testUfhuikjl->getSum($v['id'],'2023-09-01','2023-10-01');
            $retv['tenMonth'] = $testUfhuikjl->getSum($v['id'],'2023-10-01','2023-11-01');
            $retv['elevenMonth'] = $testUfhuikjl->getSum($v['id'],'2023-11-01','2023-12-01');
            $retv['twelveMonth'] = $testUfhuikjl->getSum($v['id'],'2023-12-01','2024-01-01');

            $retv['sumMonth'] = $retv['oneMonth'] + $retv['twoMonth'] + $retv['threeMonth'] + $retv['fourMonth']+
            $retv['fiveMonth']+ $retv['sixMonth'] + $retv['servenMonth'] + $retv['eightMonth'] + $retv['nineMonth'] + $retv['tenMonth']
            +$retv['elevenMonth'] + $retv['twelveMonth'];

            $ret[] = $retv;
        }
        return $ret;
    }

    /**
     * @desc 部门回款数据统计
     * @param int $id 部门id
     */
    public function departReturnSum($id)
    {
        $ret = [];
        $testUfhuikjl = new TestUfhuikjl();
        // 查询用户列表
        $whereu['departmentid'] = $id;
        // 查询部门表
        $depart = TestHrmdepartment::find($id);
        if($depart){
            $retv['departmentname'] = $depart['departmentname'];

            // 每月回款统计
            $retv['oneMonth'] = $testUfhuikjl->getDepartSum($id,'2023-01-01','2023-02-01');
            $retv['twoMonth'] = $testUfhuikjl->getDepartSum($id,'2023-02-01','2023-03-01');
            $retv['threeMonth'] = $testUfhuikjl->getDepartSum($id,'2023-03-01','2023-04-01');
            $retv['fourMonth'] = $testUfhuikjl->getDepartSum($id,'2023-04-01','2023-05-01');
            $retv['fiveMonth'] = $testUfhuikjl->getDepartSum($id,'2023-05-01','2023-06-01');
            $retv['sixMonth'] = $testUfhuikjl->getDepartSum($id,'2023-06-01','2023-07-01');
            $retv['servenMonth'] = $testUfhuikjl->getDepartSum($id,'2023-07-01','2023-08-01');
            $retv['eightMonth'] = $testUfhuikjl->getDepartSum($id,'2023-08-01','2023-09-01');
            $retv['nineMonth'] = $testUfhuikjl->getDepartSum($id,'2023-09-01','2023-10-01');
            $retv['tenMonth'] = $testUfhuikjl->getDepartSum($id,'2023-10-01','2023-11-01');
            $retv['elevenMonth'] = $testUfhuikjl->getDepartSum($id,'2023-11-01','2023-12-01');
            $retv['twelveMonth'] = $testUfhuikjl->getDepartSum($id,'2023-12-01','2024-01-01');

            $retv['sumMonth'] = $retv['oneMonth'] + $retv['twoMonth'] + $retv['threeMonth'] + $retv['fourMonth']+
                $retv['fiveMonth']+ $retv['sixMonth'] + $retv['servenMonth'] + $retv['eightMonth'] + $retv['nineMonth'] + $retv['tenMonth']
                +$retv['elevenMonth'] + $retv['twelveMonth'];

            $ret[] = $retv;
            return $ret;
        }

    }

    /**
     * @desc 部门层级列表
     * @param int $id 部门id
     */
    public function departList()
    {
        $ret = [];$rest = [];
        $where['delete_time'] = 0;
        $testHrmdepartment = TestHrmdepartment::where($where)->select()->toArray();
        foreach ($testHrmdepartment as $v) {
            $retv['id'] = $v['id'];
            $retv['name'] = $v['departmentname'];
            $retv['pid'] = $v['supdepid'];
            $rest[] = $retv;
        }
        $ret = $this->getTree($rest);
        return $ret;
    }

    /**
     * @desc 无限极分类递归
     * @param $data
     * @param int $pid
     * @param int $level
     * @return array
     */
    public function getTree($data, $pId = 0)
    {
        $tree = [];
        foreach($data as $k => $v) {
            if($v['pid'] == $pId) {
                $v['title'] = $v['name'];
                $v['children'] = $this->getTree($data, $v['id']);
                $tree[] = $v;
                unset($data[$k]);
            }
        }
        return $tree;
    }
}